package com.gec.it.web;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

import javax.servlet.http.HttpServletResponse;

import com.gec.it.entity.Doctors;
import com.gec.it.entity.Plans;
import com.gec.it.service.PlansService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.aspectj.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.gec.it.service.ApproveService;

@Controller
public class ApproveController {
	@Autowired
	private ApproveService service;
	@Autowired
	private PlansService plService;
	
	@GetMapping("/approve/ajax/comments")
	@ResponseBody
	public List<Comment> ajaxComments(int askid){
		return service.findCommentByBizkey(askid+"");
	}
	
	
	
	//打开审批列表
	@GetMapping("/approve/pass")
	public String pass(int askid,String taskid,Model m){
		Plans b = plService.getById2(askid);
		m.addAttribute("ask",b);
		
		List<String> btns = service.findOutComeListByTaskId(taskid);
		m.addAttribute("btns",btns);//把输出线的名字输出到jsp页面
		
		Task task = service.findTaskById(taskid);//显示标题
		m.addAttribute("task",task);
		
		List<Comment> cmList = service.findCommentByTask(task);
		m.addAttribute("cmList",cmList);
		return "task_pass";
	}

	//审批通过
	@PostMapping("/approve/pass")
	public String pass(int askid,String taskid,String comment,String result,Model m,@SessionAttribute("ds") Doctors d){
		Map<String,Object> map = new HashMap<String,Object>();
		Plans ask = plService.getById(askid);
		map.put("result", result);//压入变量是按钮的名字，这样就可以选择不同的方向
		map.put("base", ask.getPlanFee());
		Task next = service.saveSubmitTask(taskid, d.getUserKey(), comment, map);
		
		if(next!=null){
			ask.setPlanWfState(next.getName());
		}else{
			ask.setPlanWfState("完成");
		}
		plService.updateById(ask);
		return "redirect:/approve/mytask";
	}
	
	
	
	
	//显示任务列表（包含asks表的数据和工作流act_ru_task的数据）
	@GetMapping("/flow_mytasks")
	public String showMyTask(Model m,@SessionAttribute("ds")Doctors d){
		List<Task> taskList = service.findTaskListByName("doctors",d.getUserKey());
		List<Plans> askList = new ArrayList<Plans>();
		for(Task t:taskList){
			String bizkey = service.findBizkeyByTask(t);//增加方法，把任务对应的askid找出来
			Plans a = plService.getById2(Integer.parseInt(bizkey));
			a.setTask(t);//把task压入asks的扩充字段里
			askList.add(a);
		}
		m.addAttribute("askList",askList);
		
		//查找组任务
		List<Task> groupList = service.findGroupTaskListByName("doctors",d.getUserKey());
		List<Plans> groupAskList = new ArrayList<>();
		for(Task t:groupList){
			String bizkey = service.findBizkeyByTask(t);//增加方法，把任务对应的askid找出来
			Plans a = plService.getById2(Integer.parseInt(bizkey));
			a.setTask(t);//把task压入asks的扩充字段里
			List<String> links = service.findGroupUsersKey(t.getId());
			a.setGroupLinksName(String.join(",", links));
			groupAskList.add(a);
		}
		m.addAttribute("groupAskList",groupAskList);
		
		return "flow_mytasks";
		
	}
	
	
	//显示当前流程图页面
	@GetMapping("/approve/now")
	public String showImage(int askid, Model m){
		List<Map<String,Object>> list = service.findCoordingByBizkey("doctors", askid+"");
		m.addAttribute("list",list);
		m.addAttribute("askid",askid);
		return "flow_Image";
	}
	//为当前流程图提供图片信息
	@GetMapping("/approve/picture")
	public void picture(int askid,HttpServletResponse resp) throws IOException{
		resp.setContentType("image/jpeg");
		InputStream in = service.findImageInputStreamByBizkey("doctors", askid+"");
		OutputStream os = resp.getOutputStream();
		FileUtil.copyStream(in,os);//从数据库查出来图片，输出到客户端
		os.flush();
		os.close();
	}
	//启动流程，压入变量
	@GetMapping("/approve/start")
	public String startWorkflow(int askid,@SessionAttribute("ds")Doctors d){
		if(service.findHisProcByBizkey("doctors", askid+"")!=null){
			 throw new RuntimeException("已经开启过此工作流");
		}
		Plans a = plService.getById(askid);
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userkey", d.getUserKey());//当前用户
		map.put("base", a.getPlanFee());
		service.startWorkflow("doctors",askid,map);
		//同时修改业务对象的状态
		a.setPlanWfState("准备提交");
		plService.updateById(a);
		return "redirect:/approve/mytask";//跳转到我的任务
	}

	//打开上传流程图页面
	@GetMapping("/flow_add")
	public String deploy(){
		return "flow_add";
	}

	//上传流程图
	@PostMapping("/flow_add")
	public String deploy(String name,MultipartFile design) throws IOException{
		service.deploy(name, design.getInputStream());
		return "redirect:/deploy_list";
	}

	//展示流程图列表
	@GetMapping("/deploy_list")
	public String deployList(Model m){
		List<Deployment> list=service.findDeploys();
		m.addAttribute("list",list);
		return "admin_design_list";
	}

	//删除流程图
	@GetMapping("/admin/del")
	public String del(String id){
		service.removeDeploy(id);
		return "redirect:/admin/deploy_list";
	}
	
}



